package com.sayyi.demo.swagger.config;

import com.sayyi.demo.swagger.common.GlobalException;
import com.sayyi.demo.swagger.common.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author xuchuang
 * date 2021/12/23 4:29 PM
 */
@RestControllerAdvice
@Slf4j
public class ControllerExceptionHandler {

    @ExceptionHandler(GlobalException.class)
    @ResponseStatus
    public ResultBean<?> globalException(GlobalException globalException) {
        log.error("自定义异常", globalException);
        return ResultBean.error(globalException.getStatus());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus
    public ResultBean<?> validException(MethodArgumentNotValidException validException) {
        log.error("数据校验不通过", validException);
        return ResultBean.error(validException.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus
    public ResultBean<?> exception(Exception exception) {
        log.error("系统内部异常", exception);
        return ResultBean.error();
    }
}
